<!--
  Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

  https://www.mall4j.com/

  未经允许，不可做商业用途！

  版权所有，侵权必究！
-->

<template>
  <div class="Mall4j page-notice-list">
    <div class="content">
      <div class="crumbs">
        <router-link
          v-if="shopId === 0"
          to="/"
          class="item-a"
        >
          {{ $t('commonHead.home') }}
        </router-link>
        <router-link
          v-else
          :to="{
            path: '/shop-index',
            query: {
              sid: shopId,
              shopName
            }
          }"
          class="item-a"
        >
          {{ shopName }}-{{ $t('freeShop.storeHome') }}
        </router-link>
        <span class="arrow">></span>
        <span class="item-span">{{ shopId === 0 ? $t('lanhaiHeadlines') : $t('storeAnnouncement') }}</span>
      </div>
      <div class="list">
        <div
          v-for="item in noticeList"
          :key="item.id"
          class="item"
          @click="goNoticeDet(item.id)"
        >
          <div class="tit">
            {{ item.title }}
          </div>
          <div class="time">
            {{ item.publishTime }}
          </div>
        </div>
      </div>
      <div
        v-if="isEmpty"
        class="empty-tips base"
      >
        <img
          src="@/assets/images/empty-pic/not-found.png"
          alt
        >
        <p>{{ $t('shopInfo.noData') }}</p>
      </div>
      <!-- 页码 -->
      <pagination
        v-model="current"
        :pages="pages"
        @change-page="getNoticeList"
      />
      <!-- /页码 -->
    </div>
  </div>
</template>

<script setup>
import Cookie from 'vue-cookies'

const route = useRoute()
const router = useRouter()

const current = ref(Number(route.query.current) || 1) // 当前页数
const shopId = ref(Number(route.query.shopId) || 0)

onMounted(() => {
  document.title = shopId.value === 0 ? $t('lanhaiHeadlines') : $t('storeAnnouncement')
  getNoticeList(1)
})

const isEmpty = ref(false)
const noticeList = ref([]) // 公告列表
const pages = ref(1) // 总页数
// 获取公告列表
const getNoticeList = (cur) => {
  isEmpty.value = false
  let path = '/notice/noticeList/'
  if (Cookie.get('bbcToken')) {
    path = '/p/notice/noticeList/'
  }
  http.get(path + shopId.value, {
    params: {
      current: cur, // 当前页
      size: 10
    }
  }).then(({ data }) => {
    noticeList.value = data.records
    isEmpty.value = !noticeList.value.length
    pages.value = data.pages
  })
}

const shopName = ref(route.query.shopName || '')
// 去公告详情
const goNoticeDet = (id) => {
  router.push({
    path: '/notice-detail',
    query: {
      id,
      shopId: shopId.value,
      shopName: shopName.value
    }
  })
}

</script>

<style lang="scss" scoped>
@use "index";
</style>
